package com.lightjet.macross.base.awf.config;

import com.lightjet.macross.base.awf.service.AWFEventListener;
import com.lightjet.macross.base.awf.service.impl.TaskCreateListener;
import io.netty.util.internal.StringUtil;
import lombok.extern.java.Log;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.delegate.event.impl.ActivitiEntityEventImpl;
import org.activiti.engine.delegate.event.impl.ActivitiEventBuilder;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.text.MessageFormat;

/**
 * @author : lijia
 * @version : 1.0 2019-02-19 17:24
 * description :
 */
@Log
@Component
public class ComActivitiEventListener implements ActivitiEventListener, Serializable {
    //事件及事件的处理器
    @Override
    public void onEvent(ActivitiEvent event) {
        log.info(MessageFormat.format("{0}:{1}--{2}",event.getProcessInstanceId(),event.getExecutionId(),event.getType()));
        if(event.getType() == ActivitiEventType.TASK_CREATED)
        {
            new TaskCreateListener().handle(event);
        }
        /*ActivitiEntityEventImpl eventImpl=(ActivitiEntityEventImpl)event;
        TaskEntity taskEntity=(TaskEntity)eventImpl.getEntity();*/
    }
    @Override
    public boolean isFailOnException() {
    // TODO Auto-generated method stub
        return false;
    }

    public void publishAssignEvent(TaskEntity taskEntity){
        if (!StringUtil.isNullOrEmpty(taskEntity.getAssignee())) {
            Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
                    ActivitiEventBuilder.createEntityEvent(ActivitiEventType.TASK_ASSIGNED, taskEntity));
        }
    }
}
